package sketch.specs;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.SimpleName;

import sketch.ast.ASTFormatter;
import sketch.ast.ASTUtils;
import sketch.util.Checker;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class RandomSymbolicTreeMapGenTest extends TestCase {
	
	public static Test suite() {
		return new TestSuite(RandomSymbolicTreeMapGenTest.class);
	}
	
	public void testGenerateTestForTreeMap() {
		String filePath = "./testfiles/sketch/specs/TreeMapSimpleRandom.java";
		CompilationUnit unit = ASTUtils.astRoot(filePath);
		RandomValueVisitor visitor = new RandomValueVisitor();
		unit.accept(visitor);
		//System.out.println(unit.toString());
		//assertTrue(visitor.getNumOfReplace() == 7);
		Map<MethodInvocation, SimpleName> values = visitor.getRandomValues();
		assertEquals(1, values.size());
		
		Map<MethodInvocation, String> randomSequences = new LinkedHashMap<MethodInvocation, String>();
		
		for(MethodInvocation methodInvoke : values.keySet()) {
			String simpleName = values.get(methodInvoke).getIdentifier();
			List<Class<?>> clazz = RandomValueVisitor.getClasses(methodInvoke);
			Checker.checkTrue(!clazz.isEmpty(), "The clazz list should not be empty, for invocation: " + methodInvoke);
			Class<?> mainClass = clazz.get(0);
			Class<?>[] others = (Class<?>[])clazz.subList(1, clazz.size()).toArray(new Class<?>[0]);
			RandomSequenceCreator sequenceCreator = new RandomSequenceCreator(mainClass, others);
			String seqTxt = sequenceCreator.getSequenceTxt(simpleName);
			//System.out.println(seqTxt);
			
			randomSequences.put(methodInvoke, seqTxt);
		}
		
		System.out.println("replacing...");
		
		RandomSequenceReplaceVisitor replaceVisitor = new RandomSequenceReplaceVisitor(values, randomSequences);
		unit.accept(replaceVisitor);
		
		System.out.println("Let's see the results.");
		
		System.out.println(ASTFormatter.formatUnit(replaceVisitor.getResult()));
	}
	
}
